<template>
  <div class="register-container">
    <div class="register-form-wrapper">
      <div class="register-header">
        <img src="@/assets/logo.svg" alt="Logo" class="logo" />
        <h1 class="title">用户注册</h1>
        <p class="subtitle">创建您的账号，开始使用 Han Picture</p>
      </div>

      <a-form
        :model="registerForm"
        :rules="registerRules"
        @finish="handleRegister"
        class="register-form"
        layout="vertical"
        size="large"
      >
        <a-form-item label="用户账号" name="userAccount">
          <a-input
            v-model:value="registerForm.userAccount"
            placeholder="请输入用户账号（4-16位字母数字）"
            :prefix="h(UserOutlined)"
          />
        </a-form-item>

        <a-form-item label="密码" name="userPassword">
          <a-input-password
            v-model:value="registerForm.userPassword"
            placeholder="请输入密码（8-20位）"
            :prefix="h(LockOutlined)"
          />
        </a-form-item>

        <a-form-item label="确认密码" name="checkPassword">
          <a-input-password
            v-model:value="registerForm.checkPassword"
            placeholder="请再次输入密码"
            :prefix="h(LockOutlined)"
            @pressEnter="handleRegister"
          />
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            :loading="userStore.loading"
            block
            class="register-button"
          >
            注册
          </a-button>
        </a-form-item>

        <div class="register-footer">
          <span>已有账号？</span>
          <router-link to="/login" class="login-link"> 立即登录 </router-link>
        </div>
      </a-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, h } from 'vue'
import { useRouter } from 'vue-router'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import type { UserRegisterRequest } from '@/types/user'
import type { RuleObject } from 'ant-design-vue/es/form/interface'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 表单数据
const registerForm = ref<UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

// 自定义验证规则
const validatePassword = (_rule: RuleObject, value: string) => {
  if (!value) {
    return Promise.reject('请输入密码')
  }
  if (value.length < 8 || value.length > 20) {
    return Promise.reject('密码长度应为8-20位')
  }
  return Promise.resolve()
}

const validateCheckPassword = (_rule: RuleObject, value: string) => {
  if (!value) {
    return Promise.reject('请确认密码')
  }
  if (value !== registerForm.value.userPassword) {
    return Promise.reject('两次输入的密码不一致')
  }
  return Promise.resolve()
}

const validateUserAccount = (_rule: RuleObject, value: string) => {
  if (!value) {
    return Promise.reject('请输入用户账号')
  }
  if (value.length < 6 || value.length > 16) {
    return Promise.reject('账号长度应为6-16位')
  }
  if (!/^[a-zA-Z0-9]+$/.test(value)) {
    return Promise.reject('账号只能包含字母和数字')
  }
  return Promise.resolve()
}

// 表单验证规则
const registerRules = {
  userAccount: [{ validator: validateUserAccount, trigger: 'blur' }],
  userPassword: [{ validator: validatePassword, trigger: 'blur' }],
  checkPassword: [{ validator: validateCheckPassword, trigger: 'blur' }],
}

// 处理注册
const handleRegister = async () => {
  try {
    await userStore.register(registerForm.value)
    // 注册成功后跳转到登录页
    router.push('/login')
  } catch (error) {
    console.error('注册失败:', error)
  }
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #eeeeee 0%, #ffffff 100%);
  padding: 20px;
}

.register-form-wrapper {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
}

.register-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #262626;
  margin-bottom: 8px;
}

.subtitle {
  color: #8c8c8c;
  font-size: 16px;
  margin: 0;
}

.register-form {
  margin-top: 32px;
}

.register-button {
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
}

.register-footer {
  text-align: center;
  margin-top: 24px;
  color: #8c8c8c;
}

.login-link {
  color: #1890ff;
  text-decoration: none;
  margin-left: 8px;
}

.login-link:hover {
  color: #40a9ff;
  text-decoration: underline;
}

:deep(.ant-form-item-label > label) {
  font-weight: 500;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: 8px;
}

:deep(.ant-input) {
  border-radius: 8px;
}
</style>
